Public Class frmDelegates

    Private _DelegateHelper As Delegates
    Private _groupingAttribute As String

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        _DelegateHelper = Delegates.GetInstance

        With Me.ToolStripComboBoxGroupBy.Items
            .Clear()
            .Add("Delegators to Delegates")
            .Add("Delegates to Delegators")
        End With

        BuildDefaultView()

    End Sub

    Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick

        Dim ds As DataSet = _DelegateHelper.GetDelegatorsToDelegates

        Dim dv As DataView = New DataView(ds.Tables("Delegates"))

        If e.Node.Tag <> "All" Then
            dv.RowFilter = "Delegator = '" & e.Node.Tag & "'"
        End If

        dv.Sort = "Delegator, cn"

        DataGridView1.DataSource = dv
        Me.DetailsStatusLabel.Text = dv.Count & " delegates"

    End Sub

    Private Sub ToolStripComboBoxGroupBy_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripComboBoxGroupBy.SelectedIndexChanged

        'Cursor.Current = Cursors.WaitCursor

        ''clear the data grid
        'DataGridView1.DataSource = Nothing

        '_groupingAttribute = Me.ToolStripComboBoxGroupBy.Text

        'If _groupingAttribute <> "" Then
        '    'BuildGroupedView(_groupingAttribute)
        'Else
        '    BuildDefaultView()
        'End If

        'Cursor.Current = Cursors.Default

    End Sub

    Private Sub ToolStripRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripRefresh.Click

        'Cursor.Current = Cursors.WaitCursor

        ''_Items.Refresh()

        '_groupingAttribute = Me.ToolStripComboBoxGroupBy.Text

        'If _groupingAttribute <> "" Then
        '    'BuildGroupedView(_groupingAttribute)
        'Else
        '    BuildDefaultView()
        'End If

        'Cursor.Current = Cursors.Default

    End Sub

#Region "Core Functions"

    Private Sub BuildDefaultView()

        Dim ds As DataSet = _DelegateHelper.GetDelegatorsToDelegates

        Dim dvDelegators As DataView = New DataView(ds.Tables("Delegators"))

        dvDelegators.Sort = "cn"

        TreeView1.Nodes.Clear()

        'loop through the first grouping
        Dim drv As DataRowView
        For Each drv In dvDelegators

            Dim mynode As New TreeNode

            With mynode
                .Text = drv("cn")
                .Tag = drv("cn")
                .ImageIndex = 0
                .SelectedImageIndex = 1
            End With

            TreeView1.Nodes.Add(mynode)
        Next

        Dim allNode As TreeNode = TreeView1.Nodes.Add("All")
        allNode.Tag = "All"

        TreeView1.SelectedNode = allNode

        Me.GroupStatusLabel1.Text = dvDelegators.Count.ToString & " groups"

        'load the initial data grid with all target accounts
        Dim dv As DataView = New DataView(ds.Tables("Delegates"))
        dv.Sort = "Delegator, cn"

        DataGridView1.DataSource = dv

        Me.DetailsStatusLabel.Text = dv.Count & " accounts"

    End Sub

    'Private Sub BuildGroupedView(ByVal GroupedAttribute As String)

    '    'get the dataset...
    '    _Items.SetGrouping(GroupedAttribute)

    '    Dim dvGrouping As DataView = New DataView(_Items.DataSet.Tables("Grouping1"))
    '    dvGrouping.Sort = dvGrouping.Table.Columns(0).ColumnName

    '    TreeView1.Nodes.Clear()

    '    'loop through the first grouping
    '    Dim dr As DataRow
    '    For Each dr In dvGrouping.ToTable.Rows
    '        Dim value As String
    '        If Not IsDBNull(dr(0)) Then
    '            value = dr(0)
    '        Else
    '            value = "<Not Set>"
    '        End If

    '        Dim mynode As New TreeNode

    '        With mynode
    '            .Text = value
    '            .Tag = "Group-" & value
    '            .ImageIndex = 0
    '            .SelectedImageIndex = 1
    '        End With

    '        TreeView1.Nodes.Add(mynode)
    '    Next

    '    Me.GroupStatusLabel1.Text = dvGrouping.Count.ToString & " groups"

    '    'add details

    'End Sub

#End Region


End Class